package org.marsdonne.auth;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.marsdonne.archives.Platform;
import org.marsdonne.commons.BaseEntity;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.List;

@Getter
@Setter
@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {"platform", "platformId"}))
@NoArgsConstructor
public class PlatformUser extends BaseEntity {

    @Column(nullable = false)
    private Platform platform;

    @Column(nullable = false)
    private String platformId; // e.g. wechat unionId

    private String phoneNumber;

    private String invoiceTitle;

    @ManyToOne
    private UnionUser unionUser;

    @OneToMany(targetEntity = EndPointUser.class, mappedBy = "platformUser")
    @PrimaryKeyJoinColumn
    private List<EndPointUser> endPointUsers;

    public PlatformUser(Platform platform, String platformId, String phoneNumber, String invoiceTitle, List<EndPointUser> endPointUsers) {
        this.platform = platform;
        this.platformId = platformId;
        this.phoneNumber = phoneNumber;
        this.invoiceTitle = invoiceTitle;
        this.endPointUsers = endPointUsers;
    }
}
